package com.woniuxy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.dao.MessageDao;
import com.woniuxy.dao.OrdersDao;
import com.woniuxy.dao.RemarkDao;
import com.woniuxy.entity.Orders;
import com.woniuxy.entity.Remark;
import com.woniuxy.service.RemarkServive;
import com.woniuxy.util.CommonUtil;

@Service
public class RemarkServiceImpl implements RemarkServive {
	@Autowired
	private RemarkDao remarkDao;
	@Autowired
	private OrdersDao ordersDao;
	@Autowired
	private MessageDao messageDao;
	
	/**
	 * @author 曾令忠
	 * 添加评论
	 */
	@Override
	@Transactional
	public int addRemark(long oid, long id, String mark, String content) {
		Orders order = ordersDao.getOrderById((int)oid);
		Remark remark = new Remark();
		long userIdTo;
		if(order.getUserIdStudent()==id) {
			userIdTo = order.getUserIdCoach();
		}else {
			userIdTo= order.getUserIdStudent();
		}
		Remark r = remarkDao.getRemarkByOrderIdAndUserId(id,userIdTo,oid);
		if(r!=null) {
			return 3;
		}
		remark.setUserIdFrom(id);
		remark.setUserIdTo(userIdTo);
		remark.setMark(mark);
		remark.setContent(content);
		remark.setRemarkStatus("1");
		remark.setRemarkTime(CommonUtil.getCurrentTime());
		remark.setOrderId(oid);
		remarkDao.addRemark(remark);
		//如果是学员评论更改订单状态
		if(id==order.getUserIdStudent()) {
			ordersDao.updateStatus((int)order.getId(), "5");
		}
		//将有该orderId消息的状态标记为已读
		messageDao.updateStatusByOrderId(oid);
		return 1;
	}

}
